import { DatePickerComponent } from './date-picker.component';
import { Component, OnInit, OnChanges, EventEmitter, SimpleChanges, Input, Output } from '@angular/core';
import { dropAnimation } from '../shared/animation/index';

@Component({
  selector: 'el-date-picker-panel',
  templateUrl: './date-picker-panel.component.html',
  animations: [dropAnimation],
})
export class DatePickerPanelComponent implements OnInit, OnChanges {
  root: any;
  @Input() show: boolean = false;
  @Input() width: number = 0;
  @Input() model: number = 0;
  @Input('hidden-day') hiddenDay: boolean = false;
  @Input('panel-absolute') panelAbsolute: boolean = true;
  @Input('panel-index') panelIndex: number = 200;
  @Output() modelChange: EventEmitter<number> = new EventEmitter();
  @Input() shortcuts: boolean = false;
  @Input() showTime: boolean = false;
  @Input() currentView: string = 'date';
  @Input() dateShowModels: any;
  @Input('disabled-date') disabledDate:boolean=false;
  constructor(root: DatePickerComponent) {
    this.root = root;
  };

  showPicker(pickPath: string): void {
    this.currentView = pickPath;
  };


  updateDate(): void {
    var /** @type {?} */ date = new Date(this.model);
    var /** @type {?} */ startYear = ~~(date.getFullYear() / 10) * 10;
    this.dateShowModels = {
      month: date.getMonth(),
      year: date.getFullYear(),
      yearRange: [startYear, startYear + 10],
    };
  };


  datePickChangeHandle(time: number): void {
    this.model = time;
    this.modelChange.emit(time);
    this.updateDate();
  };


  monthPickChangeHandle(time: number): void {
    this.model = time;
    // hidden day, only show month
    if (this.hiddenDay) {
      this.modelChange.emit(time);
    }
    else {
      this.currentView = 'date';
    }
    this.updateDate();
  };


  yearPickChangeHandle(time: number): void {
    this.model = time;
    this.currentView = 'month';
    this.updateDate();
  };


  nextYear(step: number): void {
    if (this.currentView === 'year') {
      step = step * 10;
    }
    var /** @type {?} */ date = new Date(this.model);
    date.setFullYear(this.dateShowModels.year + step);
    this.model = date.getTime();
    this.updateDate();
  };


  nextMonth(step: number): void {
    var /** @type {?} */ date = new Date(this.model);
    date.setMonth(this.dateShowModels.month + step);
    this.model = date.getTime();
    this.updateDate();
  };


  ngOnInit(): void {
    this.currentView = this.hiddenDay ? 'month' : 'date';
    this.model = this.model || Date.now();
    this.updateDate();
  };


  ngOnChanges(changes: SimpleChanges): void {
    if (!changes || !changes.model)
      return;
    // first change
    if (changes.model.isFirstChange())
      return;
    this.model = changes.model.currentValue;
    this.model = this.model || Date.now();
    this.updateDate();
  };

  currentViewFuc(str:string){
    return this.currentView === str;
  }
}
